package com.googlecode.droiddip;

import android.graphics.Path;
import android.graphics.Point;
import java.util.Vector;
import android.util.Log;
//~ import java.lang.Boolean;

class Border extends Path {
	
	public Point[] points;
	
	public Border (Point... lPoints) {
		super();
		points = lPoints;
		Point startPoint = points[0];
		Point endPoint = points[points.length-1];
		this.moveTo(startPoint.x, startPoint.y);
		for (int i=1; i < points.length; ++i) {
			this.lineTo(points[i].x,points[i].y);
		}	
	}
	
	public void join( Vector<Point> newPoints)
	{
		Log.i("Border","start point: " + points[0].toString() );
		Log.i("Border","end point: " + points[points.length-1].toString() );
		Log.i("Border","start newPoint: " + newPoints.firstElement() .toString() );
		Log.i("Border","end newPoint: " + newPoints.lastElement() .toString() );
		if ( points[0].equals(newPoints.firstElement()) )
		{
			Log.i("Border","Linked first to first: " + points[0].toString());
			for (int i = 1; i < points.length; ++i )
				newPoints.insertElementAt(points[i], 0);
		}
		else if ( points[0].equals(newPoints.lastElement() ))
		{
			Log.i("Border","Linked first to last: " + points[0].toString());
			for (int i = 1; i < points.length; ++i )
				newPoints.addElement(points[i]);
		}
		else if ( points[points.length-1].equals(newPoints.firstElement()) )
		{
			Log.i("Border","Linked last to first: " + points[points.length-1].toString());
			for ( int i = points.length - 2; i >= 0; --i )
				newPoints.insertElementAt(points[i], 0);
		}
		else if ( points[points.length-1].equals(newPoints.lastElement()) )
		{
			Log.i("Border","Linked last to last: " + points[points.length-1].toString());
			for ( int i = points.length - 2; i >= 0; --i )
				newPoints.addElement(points[i]);
		} else
			Log.e("Border","failed to link borders");
	}
	
	public Vector<Point> getVector()
	{
		Vector<Point> vector = new Vector<Point>();
		vector.addElement(points[0]);
		this.join(vector);
		return vector;
	}
	
	//~ public Point[] getPoints(Point endPoint)
	//~ {
		//~ if ( endPoint == points[0]  )
			//~ return points;
		//~ else if ( endPoint == points[points.length-1] )
		//~ {
			//~ Point[] reversePoints = new Point[points.length];
			//~ for ( int i = 0; i < points.length; ++i )
			//~ {
				//~ reversePoints[i] = points[points.length-1];
			//~ }
			//~ return reversePoints;
		//~ } else
			//~ return null;
	//~ }
	
	public static Border createFromString(String pointsText) {
		String[] points1 = pointsText.split(" ");
		String[] points2;
		Point[] points = new Point[points1.length];
		
		for (int i=0; i < points1.length; ++i) {
			points2 = points1[i].split(",");
			points[i] = new Point(Integer.parseInt(points2[0]),Integer.parseInt(points2[1]));
		}
		
		return new Border(points);
		
	}	

}
